package com.ptswitch.material.pages.contract.supplier;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.SplitPageInfo;

import com.ptswitch.material.model.contract.ContractSupplier;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.ContractSupplierService;
import com.ptswitch.material.util.security.PermissionUtil;

public class ListSupplier extends PageBase {
    @Inject
    private ContractSupplierService contractSupplierSer;
    @Property
    private List<ContractSupplier> list;
    @Property
    private SplitPageInfo page = new SplitPageInfo();

    Object withInit(long size, long first) {
        this.init(size, first);
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long size, long first) {
        checkPermitted(PermissionUtil.ContractCreate);
        page.setPageSize(size);
        page.setCurrentPage(first);
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return new Object[] { page.getPageSize(), page.getCurrentPage() };
    }

    @SetupRender
    void display() {
        page.setRowCount(contractSupplierSer.countAll());
        list = contractSupplierSer.findAll(page.getCurrentRow(), page.getPageSize());
    }
}
